package com.txunda.zhuohired.fgt.user;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.find.FindBean;
import com.txunda.zhuohired.fgt.user.find.FindInformationFgt;
import com.txunda.zhuohired.http.pst.MessagePst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/26 14:57
 * 功能描述：发现页
 * 联系方式：jiahuanchen@126.com
 */

public class FindFgt extends BaseFgt {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_fgt_find_center)
    private TextView tv_layout_fgt_find_center;
    /**
     * SlidingTabLayout
     */
    @ViewInject(R.id.st_fgt_find)
    private SlidingTabLayout st_fgt_find;
    /**
     * ViewPager
     */
    @ViewInject(R.id.vp_fgt_find)
    private ViewPager vp_fgt_find;

    private ArrayList<Fragment> mFragments;
    private MyPagerAdapter myPagerAdapter;

    private List<FindBean.DataBean.TypeBean> tittleList;
    private MessagePst messagePst;
    private String cate_id = "";

    @Override
    protected void immersionInit() {
        showStatusBar(R.id.layout_fgt_find_head);
        tv_layout_fgt_find_center.setText("发现");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_find;
    }

    @Override
    protected void initialized() {
        messagePst = new MessagePst(this);
        tittleList = new ArrayList();
        mFragments = new ArrayList<>();
        myPagerAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());


    }

    @Override
    protected void requestData() {
        messagePst.findHome(cate_id);

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tittleList.get(position).getName();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("findHome")) {
            //            L.e("=========findHome=========", jsonStr);
            FindBean findBean = GsonUtil.GsonToBean(jsonStr, FindBean.class);
            tittleList = findBean.getData().getType();
            for (int y = 0; y < tittleList.size(); y++) {
                mFragments.add(FindInformationFgt.getFgt(tittleList, y));
            }
            vp_fgt_find.setAdapter(myPagerAdapter);
            st_fgt_find.setViewPager(vp_fgt_find);
            st_fgt_find.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {

                }

                @Override
                public void onTabReselect(int position) {
                }
            });
            return;
        }
    }
}
